<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $id = input('id');
        $info = db('admin')->where(['id'=>$id])->find();
      
        $info['avatar'] = 'https://'.$_SERVER['SERVER_NAME'].$info['avatar'];
        $info['image'] = 'https://'.$_SERVER['SERVER_NAME'].$info['image'];
        $this->success('请求成功',$info);
    }

    public function getsite()
    {
        $this->success('请求成功',['shenhe'=>config('site.shenhe')]);
    }

    public function getsitea()
    {
        $this->success('请求成功',['shenhe'=>config('site.shenhe_a')]);
    }
    
    public function getlist()
    {
        $ids_not = db('auth_group_access')->where('group_id',11)->column('uid');
        $ids_not[] = 1;
        $list = db('admin')->where(
            ['status'=>'normal',
            'id'=>['not in',$ids_not]
            ])->select();
        foreach ($list as $key => $value) {
            $list[$key]['avatar'] = 'https://'.$_SERVER['SERVER_NAME'].$value['avatar'];
        }
        $this->success('请求成功',$list);
    }

    #提交报名#
    public function submit(){
        $post = input('post.');
        // if($post['name']==''){
        //     $this->error('请填写姓名');
        // }
        if(!preg_match("/^1[3456789]\d{9}$/", $post['tel'])){
            $this->error('手机号码格式不正确');
        }
        $c = db('bmlist')->where('tel',$post['tel'])->count();
        if($c > 0){
            $this->error('手机号码已存在！');
        }

        // if($post['intro']==''){
        //     $this->error('请填写描述');
        // }
        $post['createtime'] = time();
        // $r = db('bmlist')->insert($post);
        $admin_id = $post['admin_id'];
        $kf_ids = db('admin')->where('pid',$admin_id)->where('online','on')->column('id');
        $c = count($kf_ids);
        if(count($kf_ids)>0){
            $c_tmp = db('bmlist')->where('admin_id','in',$kf_ids)->count();
            $index = $c_tmp%$c;
            $admin_id = $kf_ids[$index];
        }
        $r = db('bmlist')->insert([
                'admin_id' => $admin_id,
                'intro' => $post['intro'],
                'name' => $post['name'],
                'tel' => $post['tel'],
                'createtime' => time()
            ]);
        if($r){
            $this->success('提交成功');
        }else{
            $this->error('提交失败');
        }
    }
}
